package offer;

/**
 * <p>
 * 剑指 Offer 53 - I. 在排序数组中查找数字 I
 * </p>
 *
 * @author xiaozuolin
 * @date 2021/9/28 17:16
 */
public class Offer53 {

    public int search(int[] nums, int target) {
        return helper(nums, target) - helper(nums, target - 1);
    }

    public int helper(int[] nums, int tar) {
        int i = 0;
        int j = nums.length - 1;
        while (i <= j) {
            int m = (i + j) / 2;
            if (nums[m] <= tar) {
                i = m + 1;
            } else {
                j = m - 1;
            }
        }
        return i;
    }
}
